<?php
	if (session_status() == PHP_SESSION_NONE) {
		function __autoload($class_name){
			$dirs = array(
				'../Factories/',
				'../cntrlr/',
				'../mdl/',
				'../mdl/Delete/',
				'../mdl/Select/',
				'../mdl/Update/',
				'../mdl/Insert/',
				'../'
			);
			foreach($dirs as $dir){
				if(file_exists($dir.$class_name . '.php')){
					require_once($dir.$class_name . '.php');
					return;
				}
			}
			throw new BadFunctionCallException("No such Object file");
		}

		session_start();
	}
	if (isset($_SESSION["login"]) && isset($_SESSION["order"])){
		if (isset($_GET["shopId"])){
			foreach ($_SESSION["login"]->getCompanies() as $company) {
				foreach($company->getShops() as $shop ){
					if (strcmp($shop->getId(),$_GET["shopId"])==0){
						$choosenCompany = $company;
						$choosenShop = $shop;
						break;
					}
				};
			} ;

			if (isset($_GET["finish"])){

				$db      = new InserOrder();
				$db->execute($_GET["shopId"],$_SESSION["order"]);
				$_SESSION["order"]->clear();
				echo "<br/><h2>Вашата поръчка е приета !</h2>";
			} else {
				require_once('../vw/userside/finishOrderView.php');
			}
		}

	} else {
		echo "НЕЩО НЕ Е НАРЕД !!!";
	}
?>